<template>
  <div class="main">
    <el-table
      :data="eventRecords"
      style="width: 100%"
      :row-class-name="tableRowClassName">
      <el-table-column
        prop="erno"
        label="记录编号"
        width="120">
      </el-table-column>
      <el-table-column
        prop="notnum"
        label="参赛球队数量"
        width="80">
      </el-table-column>
      <el-table-column
        prop="mtno"
        label="比赛类型编号"
        width="120">
      </el-table-column>
      <el-table-column
        prop="ename"
        label="赛事名称"
        width="80">
      </el-table-column>
      <el-table-column
        prop="stime"
        label="开始日期"
        width="120">
      </el-table-column>
      <el-table-column
        prop="etime"
        label="结束日期"
        width="120">
      </el-table-column>
      <el-table-column
        prop="year"
        label="举办年份"
        width="80">
      </el-table-column>
      <el-table-column
        prop="venue"
        label="举办地点"
        width="80">
      </el-table-column>


      <el-table-column label="操作"  width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEditEventRecords(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDeleteEventRecords(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageInfo.pageSize"
        layout="prev, pager, next, jumper"
        :total="pageInfo.itemCount">
      </el-pagination>
    </div>
    <el-button size="mini" @click="handleAddEventRecords">
      新增
    </el-button>
    <addEventRecords v-if="addDialogIsShow" ref="showAddEventRecords"></addEventRecords>
    <editEventRecords v-if="editDialogIsShow" ref="showEditEventRecords"></editEventRecords>
    <deleteEventRecords v-if="deleteDialogIsShow" ref="showdeleteEventRecords"></deleteEventRecords>

  </div>
</template>

<script>
  import api from "../../../../axios/api"
  import addEventRecords from '@/components/back/liveadmin/addEventRecords.vue'
  import editEventRecords from '@/components/back/liveadmin/editEventRecords.vue'
  import deleteEventRecords from "@/components/back/liveadmin/deleteEventRecords.vue";


  export default {
    name: "index",
    addDialogIsShow:false,
    editDialogIsShow:false,
    deleteEventRecords:false,
    components:{
      addEventRecords,editEventRecords,deleteEventRecords
    },
    data(){
      return {
        eventRecords:[],
        addDialogIsShow:false,
        editDialogIsShow:false,
        deleteDialogIsShow:false,
        currentPage: 1,
        pageInfo:{}
      }
    },
    methods: {
      dateformat:function(list){
        var datalist = list;
        for(var i = 0;i< list.length;i++){
          if(list[i].stime != null){
            datalist[i].stime = list[i].stime.substr(0,10);
          }
          if (list[i].etime != null){
            datalist[i].etime = list[i].etime.substr(0,10);
          }
         if(list[i].year != null){
           var format = new Date(list[i].year);
           datalist[i].year = format.getMonth() == 12 ? parseInt(format.getFullYear())+1 : format.getFullYear();
         }

        }
        return datalist;
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.currentPage = val;
        this.setDataFromAxios(val);
      },
      getImgUrl: function (imgsrc) {
        return  this.$store.state.host + "/upload/images/head/" + imgsrc;
      },
      tableRowClassName({row, rowIndex}) {
        if (rowIndex % 2 == 0) {
          return 'warning-row';
        } else {
          return '';
        }
        return '';
      },
      setDataFromAxios: function (currPage) {
        if(currPage!=null){
          this.currentPage = currPage;
        }
        //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
        api.setDataFromAxios("/liveAdmin/getEventRecordsByPage?currPage="+this.currentPage+"&pageSize=3", null, "get")
          .then(res => {
            console.log(res)
            this.eventRecords = this.dateformat(res.tList);
            this.pageInfo;
          });//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
      },
      handleEditUser:function(index,eventRecords){
        console.log(index + "   "+ eventRecords.uId);
        let _this = this;   //this代表方法调用者 this代表vue对象
        _this.editDialogIsShow = true;
        _this.$nextTick(() => {  //下一步操作
          _this.$refs.showEditUser.init(eventRecords);  //找到vue对象中定义ref集合（$refs就是ref集合 上面ref定义的名字叫showAddUser）调用子组件的init方法
        })
      },
      handleEditEventRecords:function(index,eventRecords){
        // console.log(index + "   "+ user.uId);
        let _this = this;   //this代表方法调用者 this代表vue对象
        _this.editDialogIsShow = true;
        _this.$nextTick(() => {  //下一步操作
          _this.$refs.showEditEventRecords.init(eventRecords);  //找到vue对象中定义ref集合（$refs就是ref集合 上面ref定义的名字叫showAddUser）调用子组件的init方法
        })
      },
      handleAddEventRecords:function(){
        let _this = this;   //this代表方法调用者 this代表vue对象
        _this.addDialogIsShow = true;
        _this.$nextTick(() => {  //下一步操作
          _this.$refs.showAddEventRecords.init();  //找到vue对象中定义ref集合（$refs就是ref集合 上面ref定义的名字叫showAddUser）调用子组件的init方法
        })
      },
      handleDeleteEventRecords:function (index, eventRecords) {
        let _this = this;
        _this.deleteDialogIsShow = true;
        _this.$nextTick(() => {
          _this.$refs.showdeleteEventRecords.init(eventRecords);
        })
      }
    },
    created:function () {
      this.setDataFromAxios();//1.创建页面的时候 调用自定义的数据请求方法  而数据请求方法把得到数据赋值给了当前页面的变量
    }
  }
</script>

<style scoped lang="stylus">
  .main
    width 1000px
    margin 0 auto
  /deep/ .warning-row  /*不写deep渲染出错*/
    background-color oldlace
</style>
